package com.lineage.server.clientpackets;

import com.lineage.echo.ClientExecutor;
import com.lineage.server.model.Instance.L1DeInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Object;
import com.lineage.server.model.L1Party;
import com.lineage.server.serverpackets.S_Message_YN;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.types.Point;
import com.lineage.server.world.World;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class C_CreateParty extends ClientBasePacket {
  private static final Log _log = LogFactory.getLog(C_CreateParty.class);
  
  public void start(byte[] decrypt, ClientExecutor client) {
    try {
      read(decrypt);
      L1PcInstance pc = client.getActiveChar();
      if (!pc.isGhost() && 
        !pc.isDead() && 
        !pc.isTeleport()) {
        int targetId;
        String name;
        int objid;
        L1Object temp;
        L1PcInstance targetPc2;
        L1Object object;
        L1PcInstance targetPc;
        L1DeInstance de;
        L1PcInstance tgpc;
        int type = readC();
        switch (type) {
          case 0:
          case 1:
            targetId = readD();
            temp = World.get().findObject(targetId);
            if (!(temp instanceof L1PcInstance))
              break; 
            targetPc = (L1PcInstance)temp;
            if (pc.getId() == targetPc.getId())
              return; 
            if (!pc.getLocation().isInScreen((Point)targetPc.getLocation()) || 
              pc.getLocation().getTileLineDistance((Point)targetPc.getLocation()) > 7) {
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(952));
              return;
            } 
            if (targetPc.isInParty()) {
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(415));
              return;
            } 
            if (!pc.isInParty()) {
              targetPc.setPartyID(pc.getId());
              targetPc.sendPackets((ServerBasePacket)new S_Message_YN(953, pc.getName()));
              break;
            } 
            if (pc.getParty().isLeader(pc)) {
              targetPc.setPartyID(pc.getId());
              targetPc.sendPackets((ServerBasePacket)new S_Message_YN(953, pc.getName()));
              break;
            } 
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(416));
            break;
          case 2:
            name = readS();
            targetPc2 = World.get().getPlayer(name);
            de = null;
            if (targetPc2 == null)
              de = C_ChatWhisper.getDe(name); 
            if (targetPc2 == null && de == null) {
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(109));
              return;
            } 
            if (targetPc2 == null) {
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(109));
              return;
            } 
            if (pc.getId() == targetPc2.getId())
              return; 
            if (!pc.getLocation().isInScreen((Point)targetPc2.getLocation()) || 
              pc.getLocation().getTileLineDistance((Point)targetPc2.getLocation()) > 7) {
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(952));
              return;
            } 
            if (targetPc2.isInChatParty()) {
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(415));
              return;
            } 
            if (!pc.isInChatParty()) {
              targetPc2.setPartyID(pc.getId());
              targetPc2.sendPackets((ServerBasePacket)new S_Message_YN(951, pc.getName()));
              break;
            } 
            if (pc.getChatParty().isLeader(pc)) {
              targetPc2.setPartyID(pc.getId());
              targetPc2.sendPackets((ServerBasePacket)new S_Message_YN(951, pc.getName()));
              break;
            } 
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(416));
            break;
          case 3:
            if (!pc.isInParty())
              break; 
            if (!pc.getParty().isLeader(pc)) {
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(1697));
              break;
            } 
            objid = readD();
            object = World.get().findObject(objid);
            if (!(object instanceof L1PcInstance))
              break; 
            tgpc = (L1PcInstance)object;
            if (tgpc.getMapId() != pc.getMapId())
              pc.sendPackets((ServerBasePacket)new S_Message_YN(1695)); 
            if (pc.getLocation().isInScreen((Point)tgpc.getLocation())) {
              HashMap<Integer, L1PcInstance> map = new HashMap<>();
              map.putAll(pc.getParty().partyUsers());
              ArrayList<L1PcInstance> newList = new ArrayList<>();
              Iterator<L1PcInstance> iterator = map.values().iterator();
              while (iterator.hasNext()) {
                L1PcInstance newpc = iterator.next();
                if (!newpc.equals(tgpc))
                  newList.add(newpc); 
              } 
              map.clear();
              pc.getParty().breakup();
              L1Party party = new L1Party();
              party.addMember(tgpc);
              Iterator<L1PcInstance> iterator2 = newList.iterator();
              while (iterator2.hasNext()) {
                L1PcInstance newpc2 = iterator2.next();
                party.addMember(newpc2);
                tgpc.sendPackets((ServerBasePacket)new S_ServerMessage(424, newpc2.getName()));
              } 
              party.msgToAll();
              newList.clear();
              break;
            } 
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(1695));
            break;
        } 
        return;
      } 
      return;
    } catch (Exception exception) {
    
    } finally {
      over();
    } 
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
